//
//  PocketViewController.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/8.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class PocketViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource {

    var remaining : UILabel = UILabel.init()
    
    var table : UITableView?
    
    var source : Array<TarghModel> = []
    
    let amount = UILabel.init()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.naviHide = false
        self.title = "我的钱包"
        self.defaultBackButton()
        configView()
        self.view.backgroundColor = UIColor.colorWithHexString(hex: "f9f9f9")
        NotificationCenter.default.addObserver(self, selector: #selector(refreshBalanc), name: NSNotification.Name("refreshBalance"), object: nil)
    }
    
    @objc func refreshBalanc() -> Void {
        Manager.shared().getBalances(success: { (model) in
            if let mod = model as? BalanceModel {
                let str = "¥" + NumberToString.intToFloatString(number: mod.balance, radix: 100)
                self.amount.text = str
                let size1 = self.amount.text!.StringSize(font: self.amount.font)
                self.amount.frame = CGRect.init(origin: CGPoint.init(x: YXDeviceWidth / 2 - size1.width / 2, y: self.amount.top()), size: size1)
            }
        }, fail: {(code) in
            
        })
    }

    func configView() -> Void {
        let head = UIView.init()
        head.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: YXDeviceWidth, height: 260 * YXHeightScale))
        head.backgroundColor = UIColor.colorWithHexString(hex: "fdfefb")
        let ti = UILabel.init()
        ti.textColor = UIColor.colorWithHexString(hex: "646464")
        ti.text = "账户余额"
        let size = "账户余额".StringSize(font: UIFont.systemFont(ofSize: 28 * YXWidthScale))
        ti.font = UIFont.systemFont(ofSize: 28 * YXWidthScale)
        ti.frame = CGRect.init(origin: CGPoint.init(x: YXDeviceWidth / 2 - size.width / 2, y: 68 * YXHeightScale), size: size)
        head.addSubview(ti)
        amount.textColor = UIColor.black
        amount.font = UIFont.boldSystemFont(ofSize: 62 * YXWidthScale)
        let size1 = "".StringSize(font: amount.font)
        amount.frame = CGRect.init(origin: CGPoint.init(x: YXDeviceWidth / 2 - size1.width / 2, y: ti.bottom() + 24 * YXHeightScale), size: size1)
        head.addSubview(amount)
        refreshBalanc()
        table = UITableView.init(frame: self.view.bounds, style: .plain)
        table!.delegate = self
        table!.dataSource = self
        table!.register(MinePocketCell.classForCoder(), forCellReuseIdentifier: "moneyCell")
        table!.separatorStyle = .singleLine
        table!.separatorInset = UIEdgeInsetsMake(0, 100 * YXWidthScale, 0, 0)
        table!.layoutMargins = UIEdgeInsetsMake(0, 100 * YXWidthScale, 0, 0)
        let line = UIView.init()
        line.frame = CGRect.init(origin: CGPoint.init(x: 100 * YXWidthScale, y: head.bottom() - YXHeightScale), size: CGSize.init(width: 1000, height: YXHeightScale))
        line.backgroundColor = table!.separatorColor
        head.addSubview(line)
        table!.tableHeaderView = head
        table!.tableFooterView = UIView.init()
        let mod1 = TarghModel.init()
        mod1.image = "icon_money_withdrawals"
        mod1.title = "我要提现"
        let mod2 = TarghModel.init()
        mod2.image = "icon_money_ecord"
        mod2.title = "提现记录"
        let mod3 = TarghModel.init()
        mod3.image = "icon_money_income"
        mod3.title = "收入记录"
        source.append(mod1)
        source.append(mod2)
        source.append(mod3)
        self.view.addSubview(table!)
    }
    
    func showWidthDrawView() -> Void {
        let window = UIApplication.shared.windows[0]
        let widthDraw = WidthdrawTrayView.init(frame: window.bounds)
        widthDraw.alipayBlock = {
            let alipayAuth = AlipayAuthViewController.init()
            alipayAuth.type = 0
            alipayAuth.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(alipayAuth, animated: true)
        }
        widthDraw.wxBlock = {
            let auth = AuthViewController.init()
            auth.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(auth, animated: true)
        }
        window.addSubview(widthDraw)
        widthDraw.popTray()
    }
    
    func showWidthDrawRecord()->Void{
        //展示提现纪录
        let widthRecord = WidthDrawRecordController.init()
        self.navigationController?.pushViewController(widthRecord, animated: true)
    }
    
    func showIncomeRecord() -> Void {
        let dt = DataViewController.init()
        dt.needMonthBtn = false
        self.navigationController?.pushViewController(dt, animated: true)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if indexPath.row == 0 {
            //我要提现
            showWidthDrawView()
        } else if indexPath.row == 1 {
            //显示提现纪录
            showWidthDrawRecord()
        }else if indexPath.row == 2 {
            //显示收入纪录
            showIncomeRecord()
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return source.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "moneyCell")! as! MinePocketCell
        cell.model = source[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.separatorInset = UIEdgeInsetsMake(0, 100 * YXWidthScale, 0, 0)
        cell.layoutMargins = UIEdgeInsetsMake(0, 100 * YXWidthScale, 0, 0)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100 * YXHeightScale
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
